import {history, post, message, get} from '@/utils/utils'

const home = {
  namespaced: true,
  state: {
    list: [],
    pageNum: 1,
    hasData: true
  },

  mutations: {
    save(state, {field, payload}) {
      if (Object.prototype.toString.call(payload) === '[object Object]') {
        state[field] = { ...state[field], ...payload }
      } else {
        state[field] = payload
      }
    }
  },

  actions: {
    getRecordList({ commit, state }, {pageNum = 1} = {}) {
      get({api: '/dop/tdopsharebatch/getDopShareBatchPage',
        data: {
          current: pageNum,
          size: 10
        },
        success: ({data}) => {
          const {recordList} = state
          const {pages, records} = data
          let list = recordList

          if (pageNum > 1) {
            list = list.concat(records)
          } else {
            list = records || []
            commit('save', {field: 'hasData', payload: true})
          }
          if (pages === pageNum || pages === 0) commit('save', {field: 'hasData', payload: false})

          commit('save', {field: 'list', payload: list || []})
          commit('save', {field: 'pageNum', payload: pageNum})
        }})
    }
  }
}

export default home
